package com.deeper.customview.ui.c4.demo.c04d11;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.deeper.customview.R;

import java.util.Random;

public class GuaGuaView extends View {
    private static final float FINGER = 50;
    private Random rnd;
    private Paint paint;
    private Paint clearPaint;
    private Bitmap bmpBuffer;
    private Canvas cvsBuffer;
    private int curX, curY;
    private static final String[] PRIZE = {
            "恭喜，您中了一等奖，奖金1亿元",
            "恭喜，您中了二等奖，奖金5000万元",
            "恭喜，您中了三等奖，奖金100元",
            "很遗憾，您没有中奖，继续加油喔",
    };


    public GuaGuaView(Context context) {
        super(context);
    }


    public GuaGuaView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        rnd = new Random();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(50);
        paint.setColor(Color.WHITE);

        clearPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        clearPaint.setStyle(Paint.Style.FILL);
        clearPaint.setStrokeWidth(FINGER);
        clearPaint.setStrokeJoin(Paint.Join.ROUND);
        clearPaint.setStrokeCap(Paint.Cap.ROUND);
        clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        //画背景
        drawBackground();
    }


    public GuaGuaView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private void drawBackground() {
        Bitmap bmpBackground = BitmapFactory.decodeResource(getResources(), R.mipmap.dog);
        Bitmap bmpBackgroundMutable = bmpBackground.copy(Bitmap.Config.ARGB_8888, true);
        //在图片上画上中奖信息
        Canvas cvsBackground = new Canvas(bmpBackgroundMutable);
        //计算出文字所占区域，将文字放在中间
        String text = PRIZE[getPrizeIndex()];
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        int x = (bmpBackgroundMutable.getWidth() - rect.width()) / 2;
        int y = (bmpBackgroundMutable.getHeight() - rect.height()) / 2;

        cvsBackground.drawText(text, x, y, paint);

        //画背景
        this.setBackground(new BitmapDrawable(getResources(), bmpBackgroundMutable));
    }


    /**
     * 随机生成中奖信息
     */
    private int getPrizeIndex() {
        return rnd.nextInt(PRIZE.length);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //初始化缓冲区
        bmpBuffer = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        cvsBuffer = new Canvas(bmpBuffer);
        //为缓冲区蒙上一层灰色
        cvsBuffer.drawColor(Color.parseColor("#FF808080"));
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bmpBuffer, 0, 0, paint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                curX = x;
                curY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                cvsBuffer.drawLine(curX, curY, x, y, clearPaint);
                invalidate();
                curX = x;
                curY = y;
                break;
            case MotionEvent.ACTION_UP:
                invalidate();
                break;
        }
        return true;
    }
}
